﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace MobileShop
{
    public partial class BasePage : System.Web.UI.Page
    {
        public string PageMap
        {
            get {
                if (string.IsNullOrEmpty((string)ViewState["PageMap"]))
                    ViewState["PageMap"] = "";
                return ViewState["PageMap"].ToString(); 
            }
            set { ViewState["PageMap"] = value; }
        }

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);

            if (Branches.CurrentBranch == null)
            {
                int branchID = int.Parse(POSSystem.BLL.Utilities.GetConfigAppSetting("DefaultBranch"));
                Branches.CurrentBranch = new Branches();
                Branches.CurrentBranch.LoadByPrimaryKey(branchID);
            }

            this.MaintainScrollPositionOnPostBack = true;
        }

        public const string CART = "cart";
        public const string PRODUCT_IMAGE = "product_image";

        public static OrderItems SelectedItems
        {
            get
            {
                if (HttpContext.Current.Session[CART] == null)
                {
                    OrderItems items = new OrderItems();
                    items.LoadByPrimaryKey(-1);//load schema
                    HttpContext.Current.Session[CART] = items;
                }
                return HttpContext.Current.Session[CART] as OrderItems;
            }
            set
            {
                HttpContext.Current.Session[CART] = value;
            }
        }

        public static decimal TotalAmount
        {
            get
            {
                SelectedItems.Filter = "";
                SelectedItems.Rewind();
                decimal s = 0;
                for (int i = 0; i < SelectedItems.RowCount; i++)
                {
                    s += SelectedItems.TotalAmount;
                    SelectedItems.MoveNext();
                }
                SelectedItems.Rewind();
                return s;
            }
        }

        public static int TotalQuantity
        {
            get
            {
                SelectedItems.Filter = "";
                SelectedItems.Rewind();
                int s = 0;
                for (int i = 0; i < SelectedItems.RowCount; i++)
                {
                    s += SelectedItems.Quantity;
                    SelectedItems.MoveNext();
                }
                SelectedItems.Rewind();
                return s;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string GetDefaultPic()
        {
            return this.Page.ResolveUrl("~/images/empty.png");
        }

        public const string SESSION_USER = "SESSION_CUSTOMER";
        public View_Customers SessionUser
        {
            get { return (View_Customers)Session[SESSION_USER]; }
            set { Session.Remove(SESSION_USER); Session[SESSION_USER] = value; }
        }
    }
}